﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Dynamic;
using Eleanor.BLL;
using Newtonsoft.Json;
using SaturniLibrary.Utility;
using SaturniLibrary.Web;

namespace Eleanor.Website.Areas.Admin.Controllers
{
    public class UserController : Controller
    {
        public UserBLL userBLL = new UserBLL();

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult DoLogin()
        {
            dynamic result = new ExpandoObject();

            string userName = Request.Form["username"];
            string password = DESEncrypt.Encrypt(Request.Form["password"], "kevin");

            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
            {
                result.status = false;
                result.statusCode = 501; 
                result.message = "用户名或者密码错误";
            }
            else
            {
                dynamic user = userBLL.GetByName(userName);
                if (user == null)
                {
                    result.status = false;
                    result.statusCode = 501;
                    result.message = "用户名或者密码错误";
                }
                else
                {
                    string encryptPassword = password;
                    if (!encryptPassword.Equals(user.Password))
                    {
                        result.status = false;
                        result.statusCode = 501;
                        result.message = "用户名或者密码错误";
                    }
                    else
                    {
                        SessionManager.Set("user", user);
                        result.status = true;
                        result.statusCode = 200;
                        result.url = "/admin/user/dashboard";
                    }
                }
            }
            return Content(JsonConvert.SerializeObject(result));
        }

        public ActionResult Dashboard()
        {
            ViewBag.TopNavigation = "dashboard";
            return View();
        }

        public ActionResult Edit()
        {
            ViewBag.TopNavigation = "user";
            ViewBag.SecondNavigation = "useradd";
            return View();
        }

        public ActionResult DoEdit()
        {
            string userName = Request.Form["username"];
            string password = Request.Form["password"];
            dynamic result = new ExpandoObject();
            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
            {
                result.status = false;
                result.message = "用户名和密码不能为空";
            }
            else
            {
                string encryptPassword = DESEncrypt.Encrypt(Request.Form["password"], "kevin");
                dynamic user = new ExpandoObject();
                user.UserName = userName;
                user.Password = encryptPassword;
                if (userBLL.Add(user) <= 0)
                {
                    result.status = false;
                    result.message = "添加失败，请联系管理员";
                }
                else
                {
                    result.status = true;
                    result.url = "/admin/user/list";
                }
            }
            return Content(JsonConvert.SerializeObject(result));
        }

        public ActionResult List()
        {
            List<dynamic> users = userBLL.GetAll();
            ViewBag.Users = users;

            ViewBag.TopNavigation = "user";
            ViewBag.SecondNavigation = "userlist";
            return View();
        }

        public ActionResult DoDelete()
        {
            int userId = int.Parse(Request.Form["userid"]);
            dynamic result = new ExpandoObject();
            if (userBLL.Delete(userId))
            {
                result.status = true;
                result.url = "/admin/user/list";
            }
            else
            {
                result.status = false;
            }
            return Content(JsonConvert.SerializeObject(result));
        }

        public ActionResult DoLogout()
        {
            SessionManager.Remove("user");
            return Redirect("/admin/user/login");
        }

        public ActionResult EditPasssword()
        {
            ViewBag.TopNavigation = "user";
            ViewBag.SecondNavigation = "EditPasssword";
            return View();
        }

        public ActionResult DoEditPasssword()
        {
            dynamic result = new ExpandoObject();
            string password = Request.Form["password"];
            if (!string.IsNullOrWhiteSpace(password))
            {
                dynamic user = SessionManager.Get("user") as ExpandoObject;
                user.Password = DESEncrypt.Encrypt(password, "kevin");
                userBLL.UpdatePassword(user);
                result.status = true;
            }
            else
            {
                result.status = false;
                result.message = "密码不能为空";
            }
            return Content(JsonConvert.SerializeObject(result));
        }
    }
}